<template>
  <div>
    <!-- 表头 -->
    <el-card>
      <el-form :model="form" inline ref="form">
        <el-form-item label="用户名称" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role_id">
          <el-select placeholder="请选择状态" v-model="form.role_id">
            <el-option value="2" label="管理员"></el-option>
            <el-option value="3" label="老师"></el-option>
            <el-option value="4" label="学生"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
          <el-button @click="reset">清除</el-button>
          <el-button type="primary" @click="add">+新增用户</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 用户列表 -->
    <el-card class="userList">
      <el-table :data="userList" border class="userList">
        <el-table-column label="序号" width="98px">
          <template slot-scope="scope">{{ pagination.pageSize*(pagination.currentPage-1)+scope.$index+1 }}</template>
        </el-table-column>
        <el-table-column label="用户名" prop="username" width="240px"></el-table-column>
        <el-table-column label="电话" prop="phone" width="200px"></el-table-column>
        <el-table-column label="邮箱" prop="email" width="220px"></el-table-column>
        <el-table-column label="角色" prop="role" width="200px"></el-table-column>
        <el-table-column label="备注" prop="remark" width="240px"></el-table-column>
        <el-table-column label="状态" width="180px">
          <template slot-scope="scope">
            <span :class="{red:scope.row.status == 0}">{{scope.row.status == 0 ?'禁用':'启用'}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="240px">
          <template slot-scope="scope">
            <el-button type="text" @click="editUser(scope.row)">编辑</el-button>
            <el-button
              @click="resetStatus(scope.row.id)"
              type="text"
            >{{scope.row.status == 1 ? '禁用':'启用'}}</el-button>
            <el-button type="text" @click="delUser(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="page">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination.currentPage"
          :page-sizes="pagination.pageSizes"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
        ></el-pagination>
      </div>
    </el-card>
    <userAdd ref="adduser"></userAdd>
  </div>
</template>

<script>
import { getUserList, setStatus, deleteUser } from "@/api/user.js";
import userAdd from './userAdd';
export default {
  components:{
    userAdd,
  },
  data() {
    return {
      form: {
        username: "", //	否	string	昵称
        email: "", //	否	string	邮箱
        role_id: "", //	否	string	角色数字 2管理员、3老师、 4学生
        // page: "", //	否	string	页码 默认为1
        // limit: "", //	否	string	页尺寸 默认为10
      },
      // 分页
      pagination: {
        currentPage: 1,
        pageSize: 10,
        pageSizes: [1, 10, 20, 30],
        total: 0,
      },
      // 用户列表
      userList: [],
    };
  },
  created() {
    this.getData();
  },
  methods: {
    // 切换当前页
    handleCurrentChange(page) {
      this.pagination.currentPage = page;
      this.getData();
    },
    // 切换页容量
    handleSizeChange(size) {
      this.pagination.pageSize = size;
      this.pagination.currentPage = 1;
      this.getData();
    },
    // 获取用户列表
    getData() {
      let query = {
        ...this.form,
        page: this.pagination.currentPage,
        limit: this.pagination.pageSize,
      };
      getUserList(query).then((res) => {
        this.userList =res.data.data.items;
        // console.log(res.data);
        this.pagination.total = res.data.data.pagination.total;
      });
    },
    // 搜索按钮
    search() {
      this.pagination.currentPage = 1;
      this.getData();
    },
    //清除按钮
    reset() {
      this.$refs.form.resetFields();
      this.search();
    },
    //设置用户状态
    resetStatus(id) {
      setStatus({ id }).then(() => {
        this.getData();
      });
    },
    //删除用户
    delUser(id) {
      this.$confirm("确定删除该用户吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteUser({ id }).then(() => {
            this.search();
          });
        })
        .catch(() => {});
    },
    //新增用户
    add(){
      this.$refs.adduser.isShow = true
      this.$refs.adduser.mode = 'add'
    },
    //编辑用户
    editUser(row){
      this.$refs.adduser.form = JSON.parse(JSON.stringify(row))
      this.$refs.adduser.isShow = true
       this.$refs.adduser.mode = 'edit'
    }
  },
};
</script>

<style lang="less">
.userList {
  // padding: 30px;
  margin-top: 30px;
  .el-table-column {
    text-align: center;
  }
  .red {
    color: red;
  }
  .page {
    margin-top: 30px;
    text-align: center;
  }
  .el-table th,
  .el-table__body td {
    text-align: center;
  }
}
</style>